
package com.hlkj.pay.controller.openapi.order;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.openapi.IOpenApiAppService;
import com.hlkj.pay.app.payment.IMerchantPaymentAppService;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.dto.merchant.MerchantAcctBalanceDto;
import com.hlkj.pay.enums.TransferOrderEnums;
import com.hlkj.pay.vo.openapi.order.req.PayOrderQueryReq;
import com.hlkj.pay.vo.openapi.order.req.PaymentCommonParamReq;
import com.hlkj.pay.vo.openapi.order.resp.PaymentApiResp;
import com.hlkj.pay.vo.order.OpenApiResult;
import com.hlkj.pay.vo.order.req.OrderCommonParamReq;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/09 11:25
 */
@Tag(name = "商户代付管理")
@RestController
@RequestMapping("/openapi/payment")
@RequiredArgsConstructor
@Slf4j
public class OpenApiOrderPaymentController {

    private final IOpenApiAppService openApiAppService;

    private final IMerchantPaymentAppService merchantPaymentAppService;

    @Operation(summary = "提交付款")
    @PostMapping("/create")
    OpenApiResult<PaymentApiResp> submitOrder(@Validated @RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            PaymentCommonParamReq paymentCommonParamReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), PaymentCommonParamReq.class);
            if (TransferOrderEnums.TRANSFER_ORDER_ENTRY_TYPE.ALIPAY.getCode().equals(paymentCommonParamReq.getEntryType())) {
                if (paymentCommonParamReq.getExtra() == null) {
                    return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), "支付宝账号需要指定 extra参数");
                }
            }
            CommonResult<PaymentApiResp> payOrderRespCommonResult = merchantPaymentAppService.submitOpenapi(paymentCommonParamReq);
            return openApiAppService.sign(payOrderRespCommonResult);
        }
        catch (Exception e) {
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "付款查询")
    @PostMapping("/query")
    OpenApiResult<PaymentApiResp> queryOrder(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        PayOrderQueryReq payOrderQueryReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), PayOrderQueryReq.class);
        CommonResult<PaymentApiResp> queryOpenApiDetail = merchantPaymentAppService.queryOpenApiDetail(payOrderQueryReq);
        return openApiAppService.sign(queryOpenApiDetail);
    }

    @Operation(summary = "账号余额查询")
    @PostMapping("/account")
    OpenApiResult<List<MerchantAcctBalanceDto>> queryAccount(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        PaymentCommonParamReq payOrderQueryReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), PaymentCommonParamReq.class);
        CommonResult<List<MerchantAcctBalanceDto>> queryAccount = merchantPaymentAppService.queryAccount(payOrderQueryReq.getMchChannelCode(), null);
        List<MerchantAcctBalanceDto> data = queryAccount.getData();
        data.stream().forEach(merchantAcctBalanceDto -> {
            merchantAcctBalanceDto.setAuthInfo(null);
            merchantAcctBalanceDto.setExtCardInfo(null);
        });
        return openApiAppService.sign(queryAccount);
    }
}
